<template>
  <div>
    <!-- 点检/维护 -->
    <el-dialog title="点检任务列表" :visible.sync="stopCheckShow" width="60%" :before-close="setparentShow" append-to-body>
      <z-table :list="taskList" :tableProps="serviceTableProps" :columns="columns">
        <template v-slot:content="v">
          <span v-if="v.key == 'file_ids'">
            <span @click.stop="gofilds(v.row)" class="icon-class text-blue">
              <svg-icon icon-class="upland" />
            </span>
          </span>
          <span v-else-if="v.key=='method'">
            {{getMatchingData(v.row[v.key],v.key)}}
          </span>
          <span v-else-if="v.key == 'pattern'">
            <span v-if="v.row[v.key] == 1">
              <template>
                <el-input :disabled="taskrowStatas ==9" v-model="v.row['input_value']"></el-input>
              </template>
            </span>
            <span v-else>
              <template>
                <el-radio :disabled="taskrowStatas ==9" v-model="v.row['select_value']" :label="true">完成</el-radio>
                <el-radio :disabled="taskrowStatas ==9" v-model="v.row['select_value']" :label="false">未完成</el-radio>
              </template>
            </span>
          </span>
          <span v-else>{{ v.row[v.key] }}</span>
        </template>
      </z-table>
      <!-- 更换的备件列表 -->
      <div style="font-weight:700;;margin-top:20px">备件更换列表</div>
      <z-table :list="replaceList" v-if="replaceList.length>0" :tableProps="serviceTableProps" style="margin-top:10px"
        :columns="replaceColumns">

      </z-table>
      <el-form style="margin-top:10px" :model="AuditData" label-width="90px" v-if='taskrowStatas ==9'>
        <el-row>
          <el-col :span="24">
            <el-form-item label="确认人审核">
              <el-select v-model="AuditData.audit_result" filterable clearable>
                <el-option v-for="o in AuditResult" :key="o.value" :label="o.label" :value="o.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="确认人意见">
              <el-input type="textarea" style="width: 100%" v-model="AuditData.audit_note"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button type="success" class="fl" @click="addReplaceShow=true" v-if="taskrowStatas !=9">更换备件</el-button>
        <el-button @click="setparentShow">取 消</el-button>
        <el-button type="warning" @click="saveTask(0)" v-if="taskrowStatas !=9">保 存</el-button>
        <el-button type="primary" @click="saveTask(1)">提 交</el-button>
      </span>
    </el-dialog>
    <!-- 保养/维护 -->
    <el-dialog title="保养任务列表" :visible.sync="maintainShow" width="60%" :before-close="setparentShow" append-to-body>
      <z-table :list="taskList" :tableProps="serviceTableProps" :columns="maintainColumns">
        <template v-slot:content="v">
          <span v-if="v.key == 'file_ids'">
            <span @click.stop="gomaintenancefilds(v.row)" class="icon-class text-blue">
              <svg-icon icon-class="upland" />
            </span>
          </span>
          <span v-else-if="v.key=='method'">
            {{getMatchingData(v.row[v.key],v.key)}}
          </span>
          <span v-else-if="v.key == 'pattern'">
            <span v-if="v.row[v.key] == 1">
              <template>
                <el-input :disabled="taskrowStatas ==9" v-model="v.row['input_value']"></el-input>
              </template>
            </span>
            <span v-else>
              <template>
                <el-radio :disabled="taskrowStatas ==9" v-model="v.row['select_value']" :label="true">完成</el-radio>
                <el-radio :disabled="taskrowStatas ==9" v-model="v.row['select_value']" :label="false">未完成</el-radio>
              </template>
            </span>
          </span>
          <span v-else>{{ v.row[v.key] }}</span>
        </template>
      </z-table>
      <!-- 更换的备件列表 -->
      <div style="font-weight:700;margin-top:20px">备件更换列表</div>
      <z-table :list="replaceList" v-if="replaceList.length>0" :tableProps="serviceTableProps" style="margin-top:10px"
        :columns="replaceColumns">

      </z-table>
      <el-form style="margin-top:10px" :model="AuditData" label-width="90px" v-if='taskrowStatas ==9'>
        <el-row>
          <el-col :span="24">
            <el-form-item label="确认人审核">
              <el-select v-model="AuditData.audit_result" filterable clearable>
                <el-option v-for="o in AuditResult" :key="o.value" :label="o.label" :value="o.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="确认人意见">
              <el-input type="textarea" style="width: 100%" v-model="AuditData.audit_note"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" class="fl" @click="addReplaceShow=true" v-if="taskrowStatas !=9">更换备件</el-button>
        <el-button @click="setparentShow">取 消</el-button>
        <el-button type="warning" @click="saveTask(0)" v-if="taskrowStatas !=9">保 存</el-button>
        <el-button type="primary" @click="saveTask(1)">提 交</el-button>
      </span>

    </el-dialog>
    <preview-files :previewFilesShow="previewFilesShow" :maintainShow='maintainShow' :taskrowStatas="taskrowStatas"
      :fildData='fildData' :taskId='taskId' :upload="upload" />
    <replacePart style="margin-top:20px" :attachmentData='attachmentData' :replaceInput="replaceInput"
      :replaceData='replaceData' :addReplaceShow='addReplaceShow' />
  </div>
</template>

<script>
import commonMixin from '@/views/_common/mixin'
import PreviewFiles from './previewFiles.vue'
import replacePart from '@/views/tpm/components/replacePart'
export default {
  name: 'taskMaintenance',
  mixins: [commonMixin],
  components: { PreviewFiles, replacePart },
  props: {
    maintainShow: {
      type: Boolean,
      default: false,
    },
    stopCheckShow: {
      type: Boolean,
      default: false,
    },
    taskrowStatas: {
      type: Number,
      default: 0,
    },
    taskList: {
      type: Array,
      default: function () {
        return []
      },
    },
    // 更换备件
    replaceData: {
      type: Object,
      default: function () {
        return {}
      },
    },
    //
    replaceList: {
      type: Array,
      default: function () {
        return []
      },
    },
  },
  data() {
    return {
      query: {
        page: 1,
        size: 10,
      },
      baseUrl: this.$api.baseUrl,
      fildData: [],
      serviceTableProps: {
        border: true,
        fixed: false,
        hideOperations: true,
      },
      columns: [
        // { title: '点检任务id', key: 'task_id' },
        // { title: '点检项id', key: 'item_id' },
        { title: '任务项内容', key: 'content' },
        { title: '任务项标准', key: 'standard' },
        { title: '点检方法', key: 'method' },
        { title: '上传附件', key: 'file_ids' },
        { title: '点检形式', key: 'pattern', width: 200, hideTooltip: true },
      ],
      maintainColumns: [
        { title: '任务项内容', key: 'content' },
        { title: '任务项标准', key: 'standard' },
        { title: '保养方法', key: 'method' },
        { title: '上传附件', key: 'file_ids' },
        { title: '保养形式', key: 'pattern', width: 200, hideTooltip: true },
      ],
      replaceColumns: [
        { title: '备件料号', key: 'item_no' },
        { title: '备件编号', key: 'serial_number' },
        { title: '更换数量', key: 'qty' },
        { title: '备注', key: 'remarks' },
        { title: '更换时间', key: 'date' },
        { title: '更换人', key: 'executor' },
      ],

      radio: null,
      taskId: '',
      // 上传附件
      previewFilesShow: false,
      upload: false,
      stopCheckOptions: [],
      maintainOptions: [],
      // 更换备件
      replaceInput: false,
      attachmentData: [
        {
          item_no: '',
          specifications: '',
          serial_number: '',
          qty: undefined,
          remarks: '',
        },
      ],
      addReplaceShow: false,
      AuditData: {
        audit_result: 'Y',
        audit_note: '',
      },
      AuditResult: [
        { label: '通过', value: 'Y' },
        { label: '不通过', value: 'N' },
      ],
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getpositions()
  },
  mounted() {},
  methods: {
    getpositions() {
      //  获取点检项
      this.$request(this.$api.dictionary + 'getdirectchildren', {
        parentType: 'spotcheckMethod',
      }).then((r) => {
        this.stopCheckOptions = r.map((i) => ({
          label: i.label,
          value: i.value * 1,
        }))
      })
      this.$request(this.$api.dictionary + 'getdirectchildren', {
        parentType: 'maintainMethod',
      }).then((r) => {
        this.maintainOptions = r.map((i) => ({
          label: i.label,
          value: i.value * 1,
        }))
      })
    },
    getMatchingData(val, code) {
      if (this.maintainShow) {
        let tmp = this.maintainOptions.find((i) => i.value == val)
        return tmp ? tmp.label : ''
      } else {
        let tmp = this.stopCheckOptions.find((i) => i.value == val)
        return tmp ? tmp.label : ''
      }
    },
    // 上传点检附件
    gofilds(v) {
      this.taskId = v.id
      let params = {
        did: v.id,
      }
      this.$request(this.baseUrl + 'Tpm/TpmSpotCheckTask/GetTaskDetailFileById', params).then((r) => {
        this.fildData = r
        this.previewFilesShow = true
        this.upload = true
      })
    },
    // 上传保养附件
    gomaintenancefilds(v) {
      this.taskId = v.id
      let params = {
        did: v.id,
      }
      this.$request(this.baseUrl + 'Tpm/TpmMaintenanceTask/GetMaintenanceTaskDetailFileById', params).then((r) => {
        this.fildData = r
        this.previewFilesShow = true
        this.upload = true
      })
    },
    // 控制父级关闭弹框
    setparentShow() {
      this.$parent.maintainShow = false
      this.$parent.stopCheckShow = false
      this.$parent.taskList = []
    },
    // 保存点检项
    saveTask(val) {
      if (this.taskrowStatas == 9) {
        this.$emit('taskAuditComfirm', this.AuditData)
      } else {
        this.$emit('saveTask', val)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.icon-class {
  font-size: 18px;
  cursor: pointer;
}
</style>